package com.sjn.user.controller;

import com.codingapi.txlcn.tc.annotation.TxTransaction;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sjn.user.config.GitConfig;
import com.sjn.user.mapper.TestMapper;
import com.sjn.user.service.ClusterFeignService;
import com.sjn.user.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sun.net.www.http.HttpClient;

/**
 * @Author: sjn
 * @Data: 2021/6/15 10:44
 */
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private GitConfig gitConfig;

    @Autowired
    private TestMapper mapper;

    @Autowired
    private FeignService feignService;

    @Autowired
    private ClusterFeignService clusterFeignService;

    @RequestMapping("")
    public Object test() {
        return gitConfig.getUsername() + ", " + gitConfig.getPassword() + ", " + gitConfig.getEnv();
    }

    @RequestMapping("heath")
    public Object heath() {
        return "OK";
    }

    @RequestMapping("sqlTest")
    public Object sqlTest() {
        int count = mapper.selectCount();
        return count;
    }

    @RequestMapping("feignTest")
//    @TxTransaction
    public Object feignTest() {

//        mapper.insert();
        Object o = feignService.sqlTest();
        System.out.println("请求结果：" + o);
        return o;
    }

    @RequestMapping("feignTest2")
    @TxTransaction
    public Object feignTest2() {
        return clusterFeignService.test();
    }

    @RequestMapping("httpClientTest")
    @HystrixCommand(fallbackMethod = "httpClientTestFallBack")
    public Object httpClientTest() {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getForObject("http://localhost:8081/test/sqlTest",String.class);
        return "OK";
    }

    private Object httpClientTestFallBack() {
        return "error";
    }

}
